<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace front\helpers;


use Overtrue\Wechat\Auth;
use Overtrue\Wechat\User;

class Wechat
{

    /**
     * 获取微信用户OpenId
     * @param string $auth_type 授权类型  默认 snsapi_base:只获取open_id ,  snsapi_userinfo:获取用户基本信息
     * @return string openid
     */
    static public function getOpenId($auth_type = 'snsapi_base')
    {
        $userInfo = \Yii::$app->session->get('userInfo');
        $wechat['appId'] = 'wxb92c2bb9c487c950';
        $wechat['appSecret'] = '4531c2e3fcf9438320af5cb85b068549';

        $auth = new Auth($wechat['appId'], $wechat['appSecret']);

        if(empty(\Yii::$app->session->get($wechat['appId'].'wechat_user'))) {
            $user = $auth->authorize(null, $auth_type, 'STATE');
            \Yii::$app->session->set($wechat['appId'].'wechat_user', $user);
        }
        $user = \Yii::$app->session->get($wechat['appId'].'wechat_user');

        return $user->get('openid');
    }

    /**
     * 通过openId获取用户信息
     * @param $openId
     * @return \Overtrue\Wechat\Utils\Bag
     */
    static public function getUserInfo($openId)
    {
        $userInfo = \Yii::$app->session->get('userInfo');
        if(($userInfo['retailerid'] == '1000011079') || (\Yii::$app->request->get('retailerid') == '1000011079')) {
            $wechat['appId'] = 'wxb92c2bb9c487c950';
            $wechat['appSecret'] = '4531c2e3fcf9438320af5cb85b068549';
        } else {
            $wechat = \Yii::$app->params['wechat'];
        }
        $user = new User($wechat['appId'], $wechat['appSecret']);

        $userInfo = $user->get($openId);

        return $userInfo;
    }


    /**
     * 获取微信用户OpenId  shopbox
     * @param string $auth_type 授权类型  默认 snsapi_base:只获取open_id ,  snsapi_userinfo:获取用户基本信息
     * @return string openid
     */
    static public function getShopboxOpenId($auth_type = 'snsapi_base')
    {
        $wechat['appId'] = 'wxb92c2bb9c487c950';
        $wechat['appSecret'] = '4531c2e3fcf9438320af5cb85b068549';

        $auth = new Auth($wechat['appId'], $wechat['appSecret']);

        if(empty(\Yii::$app->session->get($wechat['appId'].'wechat_user'))) {
            $user = $auth->authorize(null, $auth_type, 'STATE');
            \Yii::$app->session->set($wechat['appId'].'wechat_user', $user);
        }
        $user = \Yii::$app->session->get($wechat['appId'].'wechat_user');

        return $user->get('openid');
    }
    /**
     * 通过openId获取用户信息shopbox
     * @param $openId
     * @return \Overtrue\Wechat\Utils\Bag
     */
    static public function getShopboxUserInfo($openId)
    {
        $wechat['appId'] = 'wxb92c2bb9c487c950';
        $wechat['appSecret'] = '4531c2e3fcf9438320af5cb85b068549';

        $user = new User($wechat['appId'], $wechat['appSecret']);

        $userInfo = $user->get($openId);

        return $userInfo;
    }
}